package cn.midas.org.controller;

import cn.midas.basic.annotation.PreAuthorize;
import cn.midas.basic.utils.AjaxResult;
import cn.midas.basic.utils.PageList;
import cn.midas.org.domain.Department;
import cn.midas.org.query.DepartmentQuery;
import cn.midas.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
* @Description: 部门的接口类
* @Author: Mr.She
* @Version: 1.0
* @Date:  2022/6/24 10:10
*/
@RestController
@RequestMapping("/department")
@Api(value = "部门的API",description="部门相关的CRUD功能")
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;

    /**
    * @Title: loadById
    * @Description: 根据Id查询
    * @Author: Mr.She
    * @Version: 1.0
    * @Date:  2022/6/24 9:35
    * @Parameters: [id]
    * @Return cn.itsource.org.domain.Department
    */
    @GetMapping("/{id}") // 请求地址：http://localhost:8080/department/1 GET请求
    @ApiOperation(value = "通过ID查询" )
    @PreAuthorize(name = "根据id查询部门",sn = "department:loadById")
    public Department loadById(@ApiParam(value="部门表的主键",required = true) @PathVariable("id") Long id){
        return departmentService.loadById(id);
    }

    /**
    * @Title: loadAll
    * @Description: 查询所有
    * @Author: Mr.She
    * @Version: 1.0
    * @Date:  2022/6/24 9:35
    * @Parameters: []
    * @Return java.util.List<cn.itsource.org.domain.Department>
    */
    @GetMapping// 请求地址：http://localhost:8080/department GET请求
    public List<Department> loadAll(){
        return departmentService.loadAll();
    }

    /**
    * @Title: remove
    * @Description: 根据Id删除
    * @Author: Mr.She
    * @Version: 1.0
    * @Date:  2022/6/24 9:39
    * @Parameters: [id]
    * @Return cn.itsource.basic.utils.AjaxResult
    */
    @DeleteMapping("/{id}") // 请求地址：http://localhost:8080/department/1 GET请求
    public AjaxResult remove(@PathVariable("id") Long id){
        try {
            departmentService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请重试！");
        }
    }


    /**
    * @Title: addOrUpt
    * @Description: 新增/修改
    * @Author: Mr.She
    * @Version: 1.0
    * @Date:  2022/6/24 9:49
    * @Parameters: [department]
    * @Return cn.itsource.basic.utils.AjaxResult
    */
    @PutMapping
    public AjaxResult addOrUpt(@RequestBody Department department){
        try {
            if(department.getId() == null){
                departmentService.save(department);
            }else {
                departmentService.update(department);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请重试！");
        }
    }


    /**
    * @Title: queryData
    * @Description: 分页+高级查询
    * @Author: Mr.She
    * @Version: 1.0
    * @Date:  2022/6/24 9:51
    * @Parameters: [departmentQuery]
    * @Return cn.itsource.basic.utils.AjaxResult
    */
    @PostMapping
    public PageList<Department> queryData(@RequestBody DepartmentQuery departmentQuery){
        PageList<Department> data = departmentService.queryData(departmentQuery);
        return data;
    }


    /**
    * @Title: batchDel
    * @Description: 批量删除
    * @Author: Mr.She
    * @Version: 1.0
    * @Date:  2022/6/25 9:47
    * @Parameters: [ids]
    * @Return cn.itsource.basic.utils.AjaxResult
    */
    @PatchMapping
    public AjaxResult batchDel(@RequestBody Long[] ids){
        try {
            departmentService.batchDel(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请重试！");
        }
    }


    /**
    * @Title: tree
    * @Description: 获取部门树数据
    * @Author: Mr.She
    * @Version: 1.0
    * @Date:  2022/6/25 14:34
    * @Parameters: []
    * @Return java.util.List<cn.itsource.org.domain.Department>
    */
    @GetMapping("/findree")// 请求地址：http://localhost:8080/department GET请求
    public List<Department> tree(){
        return departmentService.tree();
    }

}
